﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using PMS.ENT;
using PMS.Utilities;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace PMS.DAL
{
    public class RoleDAL
    {
        #region Common
        private static RoleENT CreateRoleFromReader(IDataReader reader)
        {
            RoleENT item = new RoleENT();
            try
            {

                if (!reader.IsDBNull(reader.GetOrdinal(RoleColumns.RoleID.ToString()))) item.RoleID = Utility.IntParse(reader[RoleColumns.RoleID.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(RoleColumns.RoleName.ToString()))) item.RoleName = Utility.StringParse(reader[RoleColumns.RoleName.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(RoleColumns.CreatedBy.ToString()))) item.CreatedBy = Utility.IntParse(reader[RoleColumns.CreatedBy.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(RoleColumns.UpdateBy.ToString()))) item.UpdateBy = Utility.IntParse(reader[RoleColumns.UpdateBy.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(RoleColumns.CreatedDate.ToString()))) item.CreatedDate = Utility.DateTimeParse(reader[RoleColumns.CreatedDate.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(RoleColumns.UpdateDate.ToString()))) item.UpdateDate = Utility.DateTimeParse(reader[RoleColumns.UpdateDate.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(RoleColumns.Isdeleted.ToString()))) item.Isdeleted = Utility.IntParse(reader[RoleColumns.Isdeleted.ToString()]);

           

            }
            catch
            {
                throw;
            }
            return item;
        }
        #endregion

        #region Get
       
        public static RoleCollection GetRoleListForCMB()
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand(Procedure.procRoleGetListForCMB.ToString());

                RoleCollection listRole = new RoleCollection();

                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listRole.Add(CreateRoleFromReader(reader));
                    }
                    reader.Close();
                }

                return listRole;
            }
            catch (SqlException)
            {
                throw new DataAccessException("BLM_ERR_GetRoleListForCMB");
            }
        }

        
        #endregion
    }
}